<?php

class SiteController extends HomeController {

    public function actionIndex() {
        //Home page
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->pageTitle = 'Thẻ tiêu dùng thông minh';
        $slider = SliderDetail::model()->findAll(array(
            'condition' => 'flag=1',
            'order' => 'position ASC',
            'limit' => 100,
        ));

        $this->render('home', array(
            'slider' => $slider,
        ));
    }

    public function actiondanhmuc($url) {

        $category = Category::model()->findByUrl($url);
        $this->pageTitle = $category->catename;

        $category1 = Category::model()->findAll(array(
            'condition' => 'flag=1 and parent= ' . $category->cateid,
        ));
        $sql = 'flag=1 AND cate_id in(' . $category->cateid;

        if (!empty($category1)) {
            foreach ($category1 as $item) {
                $sql = $sql . ',' . $item->cateid;
            }
            $sql = $sql . ')';
        } else {
            $sql = 'flag=1 AND cate_id=' . $category->cateid;
        }

        $criteria = new CDbCriteria();
        $criteria->condition = $sql;
        //$criteria->order = 'rank DESC,id DESC';

        $count = Product::model()->count($criteria);
        $pages = new CPagination($count);
        //results per page
        $pages->pageSize = 30;
        $pages->applyLimit($criteria);
        $produc = Product::model()->findAll($criteria);

        $this->render('danh-muc', array(
            'produc' => $produc,
            'pages' => $pages,
            'category' => $category,
                //'sql' => $sql,
        ));
    }

    public function actionDetail($id) {


        $produc = Product::model()->findByPk($id);
        $this->pageTitle = $produc->pr_name;
        $criteria = new CDbCriteria();
        $criteria->condition = 'flag=1 AND cate_id=' . $produc->cate_id . ' AND prid<>' . $produc->prid;
        $criteria->limit = 9;
        $criteria->order = 'prid DESC';
        $items = Product::model()->findAll($criteria);
        $category = Category::model()->findByPk($produc->cate_id);
        //$info = Page::model()->findByUrl('thongtinmuahang');
        //$info = isset($info) ? $info->content : '';
        $this->render('detail', array(
            'items' => $items,
            'produc' => $produc,
            //'info' => $info,
            'category' => $category,
        ));
    }

    public function actionPage() {
        $slider = SliderDetail::model()->findAll(array(
            'condition' => 'flag=1',
            'order' => 'position ASC',
            'limit' => 100,
        ));



        if (empty($_GET['view']))
            $this->actionIndex();
        $page = Page::model()->findByUrl($_GET['view']);
// if page is not found, then run a controller with that name
        if ($page === NULL) {
            $this->render('page', array(
                'pages' => $page
            ));
            throw new CHttpException(400, 'Khong tim thay trang');
        } else {
            $this->pageTitle = $page->title;

            $this->render('page', array(
                'pages' => $page
            ));
        }
    }

    public function actionDynamiccities() {
        $data = District::model()->findAll('cityID=:cityID', array(':cityID' => (int) $_POST['cityID']));
        $value0 = 0;
        $data = CHtml::listData($data, 'disID', 'disName');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn quận/huyện -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        /*
          if (!empty($data)) {
          foreach ($data as $item) {
          echo CHtml::tag('option', array('value' => $item->disID), $item->disName, true);
          }
         */
    }

    public function actionLoginweb() {

        $email = $_POST['txtuser'];
        $pass = $_POST['txtpass'];
        $passmd5 = md5(md5($pass));
        $login = UserTb::model()->findAll(array(
            'condition' => 'flag=1 and email= "' . $email . '" and pass="' . $passmd5 . '"',
        ));
        $this->pageTitle = 'Đăng nhập';
        if (count($login) > 0) {
            $session = new CHttpSession;
            $session->open();
            $session['user_salesmart'] = $login;
            $this->render('home', array(
                
            ));
        } else {
            $this->render('home', array(
                'email'=>$email,
            ));
        }
    }

    public function actionTimkiem() {
        $cityID = $_POST['cityID'];
        $disID = $_POST['disID'];
        $key = $_POST['key'];
        $category = $_POST['danhmuc'];
        //$chuoi ='chao mung ban den voi search '.$cityID.'/'.$disID.'/key='.$key;
        //$this->render('trang', array('chuoi' => $chuoi));
        $this->pageTitle = 'Tìm kiếm';

        $like = '';
        if (!empty($key)) {
            $like = ' and pr_name LIKE "%' . $key . '%"';
        }
        $city = '';
        if (!empty($cityID)) {
            $city = ' and ctiID =' . $cityID;
        }
        $district = '';
        if (!empty($disID)) {
            $district = ' and disID =' . $disID;
        }
        $danhmuc = '';
        if (!empty($category)) {
            $category1 = Category::model()->findAll(array(
                'condition' => 'flag=1 and parent= ' . $category,
            ));
            $danhmuc = ' AND cate_id in(' . $category;

            if (!empty($category1)) {
                foreach ($category1 as $item) {
                    $danhmuc = $danhmuc . ',' . $item->cateid;
                }
                $danhmuc = $danhmuc . ')';
            } else {
                $danhmuc = ' and cate_id =' . $category;
            }
        }
        $sql = 'flag=1 ' . $danhmuc . $city . $district . $like;
        $criteria = new CDbCriteria();
        $criteria->condition = $sql;
        //$criteria->order = 'rank DESC,id DESC';

        $count = Product::model()->count($criteria);
        $pages = new CPagination($count);
        //results per page
        $pages->pageSize = 60;
        $pages->applyLimit($criteria);
        $produc = Product::model()->findAll($criteria);
        $result = 'Kết quả: tìm thấy ' . $count . ' địa điểm';
        $this->render('danh-muc', array(
            'produc' => $produc,
            'pages' => $pages,
            'result' => $result,
                //'sql' => $sql,
        ));
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

}
